﻿<UserControl x:Class="FR.SAFTAnalyserLite.Views.ProdutsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:v="clr-namespace:FR.SAFTAnalyserLite.SAFT.Validations"
             xmlns:toolkit="http://schemas.xceed.com/wpf/xaml/toolkit"
             mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300">

    <DockPanel>
        <DockPanel.Resources>
            <CollectionViewSource x:Key="products" Source="{Binding Products, IsAsync=True}">
                <CollectionViewSource.GroupDescriptions>
                    <PropertyGroupDescription PropertyName="ProductType"/>
                </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>
        </DockPanel.Resources>

        <toolkit:WatermarkTextBox 
            DockPanel.Dock="Top" Margin="3" TabIndex="2"
            Text="{Binding Filter, UpdateSourceTrigger=PropertyChanged}"
            Watermark="Filtro por: Código ou Grupo ou Descrição ou Código barras ou Tipo"/>
        
        <Button DockPanel.Dock="Bottom" Width="100" Height="25" Margin="5" Padding="3" HorizontalAlignment="Right" ToolTip="Exportar para Excel"
                Command="{Binding DoPrintCommand}"
                CommandParameter="{Binding ElementName=dataGridProducts}">
            <StackPanel Orientation="Horizontal">
                <Image Source="{StaticResource Excel}"/>
                <TextBlock Text="Produtos" Padding="5,0,0,0"/>
            </StackPanel>
        </Button>

        <DataGrid x:Name="dataGridProducts" DockPanel.Dock="Bottom" VerticalAlignment="Stretch" TabIndex="1"
                  ItemsSource="{Binding Source={StaticResource products}}"
                  SelectedItem="{Binding Product, Mode=TwoWay}" SelectionChanged="dataGridProducts_SelectionChanged">

            <DataGrid.RowValidationRules>
                <v:ProductValidationRule ValidationStep="ConvertedProposedValue" ValidatesOnTargetUpdated="True"/>
            </DataGrid.RowValidationRules>

            <DataGrid.RowValidationErrorTemplate>
                <ControlTemplate>
                    <Grid Margin="0,-2,0,-2"
                              ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}},
                            Path=(Validation.Errors)[0].ErrorContent}">
                        <Ellipse StrokeThickness="0" Fill="Red" 
                                     Width="{TemplateBinding FontSize}" 
                                     Height="{TemplateBinding FontSize}" />
                    </Grid>
                </ControlTemplate>
            </DataGrid.RowValidationErrorTemplate>

            <DataGrid.Columns>
                <DataGridTextColumn Header="Código" Binding="{Binding ProductCode}"/>
                <DataGridTextColumn Header="Grupo" Binding="{Binding ProductGroup}" />
                <DataGridTextColumn Header="Descrição" Binding="{Binding ProductDescription}" />
                <DataGridTextColumn Header="Código barras" Binding="{Binding ProductNumberCode}" />
            </DataGrid.Columns>
            <DataGrid.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding Path=Name}" />
                            </StackPanel>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander IsExpanded="True">
                                            <Expander.Header>
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock Text="{Binding Path=Name}" />
                                                    <TextBlock Text=" ( " />
                                                    <TextBlock Text="{Binding Path=ItemCount}" />
                                                    <TextBlock Text=" )" />
                                                </StackPanel>
                                            </Expander.Header>
                                            <ItemsPresenter />
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </DataGrid.GroupStyle>
        </DataGrid>

    </DockPanel>
</UserControl>
